import { reqgetcountdown, reqgetcoupon, reqgetgroupon, reqgetnew, reqgetseckill, reqShouYe } from "../../http/api"

const initState = {
    //初始化
    info: [],
    groupon: [],
    count: [],
    //秒杀
    seckill:[],
    // 获取商品信息首页
    indexgoods:[],
    n:0,
    pre: "http://localhost:1000",
}

const types = {
    //修改types
    HOME_CATE_CHANGE_INFO: "HOME_CATE_CHANGE_INFO",
    //修改types
    GROUPON: "GROUPON",
    //修改count
    COUNT: "COUNT",
    //修改秒杀
    SECKILL:"SECKILL",
    //商品信息首页
    INDEXGOODS:"INDEXGOODS",
    //修改首页的n
    CHANGE_HOME_N:"CHANGE_HOME_N"
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        //修改info的数据
        case types.HOME_CATE_CHANGE_INFO:
            return {
                ...state,
                info: action.info
            }
        //修改groupon的数据
        case types.GROUPON:
            return {
                ...state,
                groupon: action.groupon
            }
        //修改count的数据
        case types.COUNT:
            return {
                ...state,
                count: action.count
            }
        //修改seckill的数据
        case types.SECKILL:
            return{
                ...state,
                seckill:action.seckill
            }
        //修改indexgoods的数据
        case types.INDEXGOODS:
            return{
                ...state,
                indexgoods:action.indexgoods
            }
        //修改n的数据
        case types.CHANGE_HOME_N:
            return{
                ...state,
                n:action.n
            }
        default:
            return state
    }
}

export const actions = {
    //传参修改info
    changeInfo: info => ({ type: types.HOME_CATE_CHANGE_INFO, info }),
    //传参修改info
    changeGroupon: groupon => ({ type: types.GROUPON, groupon }),
    //传参修改count
    changeCount:count=>({type:types.COUNT,count}),
    //传参修改seckill
    changeSeckill:seckill=>({type:types.SECKILL,seckill}),
    //传参修改indexgoods
    changeIndexgoods:indexgoods=>({type:types.INDEXGOODS,indexgoods}),
    //传参修改n
    changeHomeN:n=>({type:types.CHANGE_HOME_N,n}),


    //请求info
    reqInfo: () => (dispatch, getState) => {
        let info = getInfo(getState())
        if (info.length > 0) {
            return;
        }
        reqgetnew().then(res => {
            if (res.data.code == 200) {
                dispatch(actions.changeInfo(res.data.list))
            }
        })
    },
    //请求groupon
    reqGroupon: () => (dispatch, getState) => {
        let groupon = getGroupon(getState())
        if (groupon.length > 0) {
            return;
        }
        reqgetgroupon().then(res => {
            if (res.data.code == 200) {
                dispatch(actions.changeGroupon(res.data.list))
            }
        })
    },
    //请求count
    reqCount:()=>(dispatch,getState)=>{
        let count = getCount(getState())
        if (count.length > 0) {
            return;
        }
        reqgetcountdown().then(res=>{
            if(res.data.code==200){
                dispatch(actions.changeCount(res.data.list))
            }
        })
    },
    //请求seckill
    reqSeckill:()=>(dispatch,getState)=>{
        let seckill = getSeckill(getState())
        if (seckill.length > 0) {
            return;
        }
        reqgetseckill().then(res=>{
            if(res.data.code==200){
                dispatch(actions.changeSeckill(res.data.list))
            }
        })
    },
    //请求indexgoods
    reqIndexgoods:()=>(dispatch,getState)=>{
        let seckill = getSeckill(getState())
        if (seckill.length > 0) {
            return;
        }
        reqShouYe().then(res=>{
            if(res.data.code==200){
                dispatch(actions.changeIndexgoods(res.data.list))
            }
        })
    }
    
}

//导出
export const getInfo = state => state.home.info
export const getPre = state => state.home.pre
export const getGroupon = state => state.home.groupon
export const getCount = state => state.home.count
export const getSeckill= state=>state.home.seckill
export const getIndexgoods=state=>state.home.indexgoods
export const getHomeN = state=>state.home.n

export default reducer;